package com.zff.rental.utils.collectors;

import java.util.function.Function;
import java.util.stream.Collector;

/**
 * 搜集器工具类
 *
 * @author HCY
 */
public class CollectorsUtils {
    /**
     * 收集Float平均值
     */
    public static <T> Collector<T, float[], Float> averagingFloat(Function<? super T, Float> mapper) {
        return Collector.of(
                () -> new float[]{0f, 0f},
                (a, t) -> {
                    a[0] += mapper.apply(t);
                    a[1]++;
                },
                (a, b) -> {
                    a[0] += b[0];
                    a[1] += b[1];
                    return a;
                },
                a -> a[1] == 0 ? 0.0f : a[0] / a[1]
        );
    }
}
